// @Author EthanScriptOn
// @Desc
package config_parser

import (
	"context"
	"errors"
	"gitee.com/fatzeng/srf_switch_basic_components/processor"
	"gitee.com/fatzeng/srf_switch_config/common"
	"gitee.com/fatzeng/srf_switch_config/config_context"
	"gitee.com/fatzeng/srf_switch_protocol/protocol/log"
)

type FundamentalParserProcessorConfigurator struct {
	processor.Processor
	ResourceParser
	common.FundamentalComponent
}

func (f *FundamentalParserProcessorConfigurator) Initialization() (err error) {
	log.Logger().Info("You can implement this function to perform some preprocessing operations")
	return
}

func (f *FundamentalParserProcessorConfigurator) Process(ctx context.Context, pendingProcessingData interface{}) (err error) {
	configInitializationContext, ok := ctx.(*config_context.ConfigInitializationContext)
	if !ok {
		return errors.New("need *switchContext.ConfigInitializationContext type config_context")
	}
	return f.ExecuteParse(configInitializationContext, pendingProcessingData)
}

func (f *FundamentalParserProcessorConfigurator) ExecuteParse(ctx *config_context.ConfigInitializationContext, pendingProcessingData interface{}) error {
	currentReferenceProxy, err := f.GetCurrentReferenceProxy(ctx)
	if err != nil {
		return err
	}
	if err = f.HandleCallErr(currentReferenceProxy.Call(ResourceParser.ParsePrepare, ctx)); err != nil {
		return err
	}
	if err = f.HandleCallErr(currentReferenceProxy.Call(ResourceParser.Parse, ctx, pendingProcessingData)); err != nil {
		return err
	}
	if err = f.HandleCallErr(currentReferenceProxy.Call(ResourceParser.ParseDoMore, ctx)); err != nil {
		return err
	}
	return f.HandleCallErr(currentReferenceProxy.Call(ResourceParser.StageParseResult, ctx))
}
